#include "util.h"
/**********
 *          NAME :GetSessionIDFromInputList
 *         CNAME :从inputlist中获得cookie值
 *   DESCRIPTION :
 *        AUTHOR : HOTMOCHA
 *  CREATED TIME : 2015-02-03 22:18:49
 *        @INPUT :
 *
 *       @OUTPUT :
 *
 *       @RETURN :
 *
 *       HISTORY :
 *
 **********/ 
int GetSessionIDFromInputList(struct SpiderEnv *env, char *strSessionID, int len)
{ 
	int isFoundSessionID = 0;            
	int ret = 0;
	ListNode *cur = env->inputmapstringlist->first;
	MapEntry *entry = NULL;
	
	while (cur) 
	{
		if (cur->data) 
		{ 
			if ((entry = (MapEntry*)cur->data) != NULL)
				if (entry->type == STRING) 
				{ 
					if (strcmp(entry->key, RENRENDAI_SESSIONDID ) == 0) 
					{ 
						if (entry->value_str->usedlen > len) {
							ErrorLog(__FILE__, __LINE__, "传入buf太小");
							return SPIDER_INTERNAL_ERR;
						}
						else {
							memcpy(strSessionID, entry->value_str->base, len); 
							isFoundSessionID = 1;   
							break;
						}
					}
				} 
		} 
	}
	if (isFoundSessionID != 1) { 
		ErrorLog(__FILE__, __LINE__, "not find session id"); 
		return -1;
	}
	return 0;
}           
int GetLoanIDFromInputList(struct SpiderEnv *env, long *pLoanId)
{

}
/**********
 *          NAME :PutOrResetDataInList
 *         CNAME :添加或者设置数据到List
 *   DESCRIPTION :
 *        AUTHOR : HOTMOCHA
 *  CREATED TIME : 2015-02-03 22:36:06
 *        @INPUT :
 *
 *       @OUTPUT :
 *
 *       @RETURN :
 *
 *       HISTORY :
 *
 **********/
int PutOrResetStringInList(ListHead *head, char *key, char *value) 
{
	int ret = 0;
	MapEntry *entry = NULL;
	if (!head) {
		return SPIDER_ARG_ERR;
	}
	/* 创建一个MapEntry */
	entry = InitMapEntry();
	if (entry == NULL)
		return SPIDER_MEM_ERR;
	ret = SetStringValue(entry, key, value);
	if (ret) {
		return SPIDER_INTERNAL_ERR;
	}

	/* 查找是否已经存在 */
	void *retData = FindDataInList(head, entry, (DataCmpFunc)MapEntryCmp);
	/* 未找到 */
	if (!retData) {
		/* INSERT */
		if (ret = InsertDataBeTheFirst(head, entry)) {
			FreeMapEntry(entry);
			return ret;
		} 
		return 0;
	}
	/* 找到 */
	else {
		/* SET */
		FreeMapEntry(entry);
		MapEntry *tempEntry = (MapEntry*)retData;
		SetStringValue(tempEntry, RENRENDAI_SESSIONDID, key);
		return 0;
	}
}
int PutOrResetStringInListInLast(ListHead *head, char *key, char *value) 
{
	int ret = 0;
	MapEntry *entry = NULL;
	if (!head) {
		return SPIDER_ARG_ERR;
	}
	/* 创建一个MapEntry */
	entry = InitMapEntry();
	if (entry == NULL)
		return SPIDER_MEM_ERR;
	ret = SetStringValue(entry, key, value);
	if (ret) {
		return SPIDER_INTERNAL_ERR;
	}

	/* 查找是否已经存在 */
	void *retData = FindDataInList(head, entry, (DataCmpFunc)MapEntryCmp);
	/* 未找到 */
	if (!retData) {
		/* INSERT */
		if (ret = InsertDataBeTheLast(head, entry)) {
			FreeMapEntry(entry);
			return ret;
		} 
		return 0;
	}
	/* 找到 */
	else {
		/* SET */
		FreeMapEntry(entry);
		MapEntry *tempEntry = (MapEntry*)retData;
		SetStringValue(tempEntry, RENRENDAI_SESSIONDID, key);
		return 0;
	}
}
/**********
 *          NAME :PutStringInList
 *         CNAME :不管有没有重复都会插入
 *   DESCRIPTION :
 *        AUTHOR : HOTMOCHA
 *  CREATED TIME : 2015-02-05 21:30:14
 *        @INPUT :
 *
 *       @OUTPUT :
 *
 *       @RETURN :
 *
 *       HISTORY :
 *
 **********/
int PutStringInList(ListHead *head, char *key, char *value) 
{
	int ret = 0;
	MapEntry *entry = NULL;
	if (!head) {
		return SPIDER_ARG_ERR;
	}
	/* 创建一个MapEntry */
	entry = InitMapEntry();
	if (entry == NULL)
		return SPIDER_MEM_ERR;
	ret = SetStringValue(entry, key, value);
	if (ret) {
		return SPIDER_INTERNAL_ERR;
	}
	/* INSERT */
	if (ret = InsertDataBeTheFirst(head, entry)) {
		FreeMapEntry(entry);
		return ret;
	} 
	return 0;
}
int PutLongInList(ListHead *head, char *key, long value) 
{
	int ret = 0;
	MapEntry *entry = NULL;
	if (!head) {
		return SPIDER_ARG_ERR;
	}
	/* 创建一个MapEntry */
	entry = InitMapEntry();
	if (entry == NULL)
		return SPIDER_MEM_ERR;
	ret = SetLongValue(entry, key, value);
	if (ret) {
		return SPIDER_INTERNAL_ERR;
	}
	/* INSERT */
	if ((ret = InsertDataBeTheFirst(head, entry)) != 0) {
		FreeMapEntry(entry);
		return ret;
	} 
	return 0;
}

int PutLongInListInLast(ListHead *head, char *key, long value) 
{
	int ret = 0;
	MapEntry *entry = NULL;
	if (!head) {
		return SPIDER_ARG_ERR;
	}
	/* 创建一个MapEntry */
	entry = InitMapEntry();
	if (entry == NULL)
		return SPIDER_MEM_ERR;
	ret = SetLongValue(entry, key, value);
	if (ret) {
		return SPIDER_INTERNAL_ERR;
	}
	/* INSERT */
	if ((ret = InsertDataBeTheLast(head, entry)) != 0) {
		FreeMapEntry(entry);
		return ret;
	} 
	return 0;
}
/**********
 *          NAME :FindKeyInList
 *         CNAME :根据key在list中查找MapEntry
 *   DESCRIPTION :
 *        AUTHOR : HOTMOCHA
 *  CREATED TIME : 2015-02-03 22:56:38
 *        @INPUT :
 *
 *       @OUTPUT :
 *
 *       @RETURN :
 *
 *       HISTORY :
 *
 **********/
int FindKeyInList(ListHead *head, char *key, MapEntry **m)
{
	int ret = 0;
	MapEntry *entry = NULL;
	if (!head || !m) {
		return SPIDER_ARG_ERR;
	}
	entry = InitMapEntry();
	if (entry == NULL) {
		return SPIDER_MEM_ERR;
	}
	if ((ret = SetStringValue(entry, key, "")) != 0){ 
		return ret;
	}
	void *retData = FindDataInList(head, entry, (DataCmpFunc)MapEntryCmp);
	/* 释放空间 */
	FreeMapEntry(entry);
	if (!retData) {
		return NOTFOUND;
	}
	else {
		*m = (MapEntry*)retData;
		return 0;
	}
}
int PopKeyInList(ListHead *head, char *key, MapEntry **m)
{
	int ret = 0;
	MapEntry *entry = NULL;
	ListNode* node = NULL;
	if (!head || !m) {
		return SPIDER_ARG_ERR;
	}
	entry = InitMapEntry();
	if (entry == NULL) {
		return SPIDER_ARG_ERR;
	}
	if ((ret = SetStringValue(entry, key, "")) != 0){ 
		return SPIDER_INTERNAL_ERR;
	}
	node = FindNodeInList(head, (void*)entry, (DataCmpFunc)MapEntryCmp);
	FreeMapEntry(entry);
	if (!node)
		return NOTFOUND;
	*m = (MapEntry*)(node->data);
	DeleteListNode(head, node);
	return 0;
}


int PrintRespHeader(struct HttpReq *req)
{
	fprintf(stderr, "-----------req[%s]--header--------------\n", req->reqname);
	fprintf(stderr, "%s\n", req->recvheader->base);
	fprintf(stderr, "+++++++++++req[%s]++header++++++++++++++\n", req->reqname);
	return 0;
}
int PrintRespBody(struct HttpReq *req)
{
	fprintf(stderr, "-----------req[%s]--body--------------\n", req->reqname);
	fprintf(stderr, "%s\n", req->recvbody->base);
	fprintf(stderr, "+++++++++++req[%s]++body++++++++++++++\n", req->reqname);
	return 0;
}
